package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Slf4j
@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper  setmealDishMapper;

    /**
     * 添加套餐
     * @param setmealDTO
     */
    @Transactional//开启事务
    @Override
    public void insertSetmeal(SetmealDTO setmealDTO) {
        //属性拷贝
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        //调用mapper保存方法
        setmealMapper.insertSetmeal(setmeal);
        //获取套餐id
        Long id = setmeal.getId();
        log.info("套餐id：{}",id);
        //构造菜品列表
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        //关联套餐id
        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(id);
        });
        //调用mapper方法批量插入中间表
        setmealDishMapper.insertBatch(setmealDishes);

    }

    /**
     * 分页查询套餐
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageSetmeal(SetmealPageQueryDTO setmealPageQueryDTO) {
        //1.设置分页参数
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        //调用mapper强转Page对象
        Page<SetmealVO> page = setmealMapper.pageSetmeal(setmealPageQueryDTO);
        //封装返回
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 根据id批量删除套餐
     * @param ids
     */
    @Transactional//开启事务
    @Override
    public void deleteSetmeal(List<Long> ids) {
        //1原型图中的业务规则：删除在售状态下，不可删除套餐
        ids.forEach(id->{
            Setmeal setmeal = setmealMapper.selectById(id);
            if (setmeal.getStatus() == StatusConstant.ENABLE){
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        });
        //2.调用mapper方法删除套餐信息
        setmealMapper.deleteSetmeal(ids);
        //3.调用mapper方法删除setmeal_dish表中的记录
        setmealDishMapper.deleteSetmealDish(ids);

    }

    /**
     * 根据id回显套餐信息
     * @param id
     * @return
     */

    @Override
    public SetmealVO getById(Long id) {
        //1.根据id查询套餐信息
        Setmeal setmeal = setmealMapper.selectById(id);
        //2.属性拷贝补充基本属性
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal,setmealVO);
        //3.根据套餐id查询套餐菜品关系
        List<SetmealDish> setmealDishes = setmealDishMapper.selectByDishId(id);
        //4.构造DishVo对象并返回
        setmealVO.setSetmealDishes(setmealDishes);
        return setmealVO;
    }
    @Transactional//开启事务这里涉及到了两个表的操作dish_setmeal 和setmeal表
    @Override
    public void updateSetmeal(SetmealDTO setmealDTO) {
        //1.创建setmeal对象
        Setmeal setmeal = new Setmeal();
        //2.拷贝基本属性
        BeanUtils.copyProperties(setmealDTO,setmeal);
        //3.调用mapper方法更新
        setmealMapper.update(setmeal);
        //4.先全部删除setmeal_dish表中的记录再一次行全部添加进去
        setmealDishMapper.deleteByDishId(setmealDTO.getId());
        //5.构造菜品列表
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        //6.关联套餐id
        setmealDishes.forEach(setmealDish ->{
            setmealDish.setSetmealId(setmealDTO.getId());
        });
        //7.调用mapper方法批量插入
        setmealDishMapper.insertBatch(setmealDishes);
    }

    /**
     * 起售and停售
     * @param status
     * @param id
     */
    @Override
    public void startOrStop(Integer status, Long id) {
        //1.创建setmeal对象
        Setmeal setmeal = new Setmeal().builder()
                .id(id)
                .status(status)
                .build();
        //复用mapper方法update
        setmealMapper.update(setmeal);
    }
    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}
